home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / archie-1.4.1 / procquery.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-18  |  6.5 KB  |  286 lines

  1. /*
  2.  * procquery.c : Routines for processing results from Archie
  3.  *
  4.  * Originally part of the Prospero Archie client by Cliff Neuman (bcn@isi.edu).
  5.  * Modified by Brendan Kehoe (brendan@cygnus.com).
  6.  * Re-modified by George Ferguson (ferguson@cs.rochester.edu).
  7.  *
  8.  * Copyright (c) 1991 by the University of Washington
  9.  *
  10.  * For copying and distribution information, please see the file
  11.  * <copyright.h>.
  12.  *
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <pfs.h>
  17. #include <perrno.h>
  18. #include <pmachine.h>
  19. #include <archie.h>
  20.  
  21. #ifdef NEED_TIME_H
  22. # include <time.h>
  23. # ifdef WANT_BOTH_TIME
  24. #  include <sys/time.h>
  25. # endif
  26. #else
  27. # ifndef VMS
  28. #  include <sys/time.h>
  29. # endif
  30. #endif
  31.  
  32. /* Defined in dirsend.c.  */
  33. extern int client_dirsrv_timeout, client_dirsrv_retry;
  34. /* In archie.c.  */
  35. extern char *program_name;
  36. extern FILE *archie_out;
  37.  
  38. /*
  39.  * Functions defined here
  40.  */
  41. static void display_link (), alex_name ();
  42. void procquery ();
  43.  
  44. /*
  45.  * Data defined here
  46.  */
  47. extern int pwarn, perrno;
  48. extern int listflag, sortflag, alex;
  49.  
  50. int pfs_debug;
  51.  
  52. static struct tm *presenttime;
  53. static char lastpath[MAX_VPATH] = "\001";
  54. static char lasthost[MAX_VPATH] = "\001";
  55.  
  56. /* Print the contents of the given virtual link.  */
  57. static void
  58. display_link (l)
  59.      VLINK l;
  60. {
  61.     PATTRIB     ap;
  62.     char    linkpath[MAX_VPATH];
  63.     int        dirflag = 0;
  64. #ifdef MSDOS
  65.     unsigned long size = 0L;
  66. #else
  67.     int        size = 0;
  68. #endif
  69.     char    *modes = "";
  70.     char    archie_date[20];
  71.     char    *gt_date = "";
  72.     int        gt_year = 0;
  73.     int        gt_mon = 0;
  74.     int        gt_day = 0;
  75.     int        gt_hour = 0;
  76.     int        gt_min = 0;
  77.     int        special = listflag || alex;
  78.  
  79.     /* Initialize local buffers */
  80.     *archie_date = '\0';
  81.  
  82.     /* Remember if we're looking at a directory */
  83.     if (sindex(l->type,"DIRECTORY"))
  84.     dirflag = 1;
  85.     else
  86.     dirflag = 0;
  87.     
  88.     /* Extract the linkpath from the filename */
  89.     strcpy(linkpath,l->filename);
  90.     *(linkpath + (strlen(linkpath) - strlen(l->name) - 1)) = '\0';
  91.     
  92.     /* Is this a new host? */
  93.     if (strcmp(l->host,lasthost) != 0) {
  94.     if (!special)
  95.         fprintf (archie_out, "\nHost %s\n\n",l->host);
  96.     strcpy(lasthost,l->host);
  97.     *lastpath = '\001';
  98.     }
  99.     
  100.     /* Is this a new linkpath (location)? */
  101.     if(strcmp(linkpath,lastpath) != 0) {
  102.     if (!special)
  103.         fprintf (archie_out,
  104.              "    Location: %s\n",(*linkpath ? linkpath : "/"));
  105.     strcpy(lastpath,linkpath);
  106.     }
  107.     
  108.     /* Parse the attibutes of this link */
  109.     for (ap = l->lattrib; ap; ap = ap->next) {
  110.     if (strcmp(ap->aname,"SIZE") == 0) {
  111. #ifdef MSDOS
  112.         sscanf(ap->value.ascii,"%lu",&size);
  113. #else
  114.         sscanf(ap->value.ascii,"%d",&size);
  115. #endif
  116.     } else if(strcmp(ap->aname,"UNIX-MODES") == 0) {
  117.         modes = ap->value.ascii;
  118.     } else if(strcmp(ap->aname,"LAST-MODIFIED") == 0) {
  119.         gt_date = ap->value.ascii;
  120.         sscanf(gt_date,"%4d%2d%2d%2d%2d",>_year,
  121.            >_mon, >_day, >_hour, >_min);
  122.         if ((12 * (presenttime->tm_year + 1900 - gt_year) + 
  123.                     presenttime->tm_mon - gt_mon) > 6) 
  124.         sprintf(archie_date,"%s %2d %4d",month_sname(gt_mon),
  125.             gt_day, gt_year);
  126.         else
  127.         sprintf(archie_date,"%s %2d %02d:%02d",month_sname(gt_mon),
  128.              gt_day, gt_hour, gt_min);
  129.     }
  130.     }
  131.     
  132.     /* Print this link's information */
  133.     if (listflag)
  134. #if defined(MSDOS)
  135.       fprintf (archie_out,
  136.            "%s %6lu %s %s%s\n",gt_date,size,l->host,l->filename,
  137.            (dirflag ? "/" : ""));
  138. #else
  139.     fprintf (archie_out,
  140.          "%s %6d %s %s%s\n",gt_date,size,l->host,l->filename,
  141.          (dirflag ? "/" : ""));
  142. #endif
  143.     else if (alex)
  144.       alex_name (l->host, l->filename, dirflag);
  145.     else
  146. #ifdef MSDOS
  147.     fprintf (archie_out,
  148.          "      %9s %s %10lu  %s  %s\n",(dirflag ? "DIRECTORY" : "FILE"),
  149.          modes,size,archie_date,l->name);
  150. #else
  151.     fprintf (archie_out,
  152.          "      %9s %s %10d  %s  %s\n",(dirflag ? "DIRECTORY" : "FILE"),
  153.          modes,size,archie_date,l->name);
  154. #endif /* MSDOS */
  155.  
  156.  
  157.     /* Free the attibutes */
  158.     atlfree(l->lattrib);
  159.     l->lattrib = NULL;
  160. }
  161.  
  162. /*    -    -    -    -    -    -    -    -    */
  163. /*
  164.  * procquery : Process the given query and display the results. If
  165.  *    sortflag is non-zero, then the results are sorted by increasing
  166.  *    date, else by host/filename. If listflag is non-zero then each
  167.  *    entry is printed on a separate, complete line. Note that listflag
  168.  *    is ignored by xarchie.
  169.  */
  170. void
  171. procquery(host,str,max_hits,offset,query)
  172. char *host,*str;
  173. int max_hits,offset;
  174. Query query;
  175. {
  176.     VLINK l;
  177.     long now;
  178.  
  179.     /* initialize data structures for this query */
  180.     (void)time(&now);
  181.     presenttime = localtime(&now);
  182.  
  183.     /* Do the query */
  184.     if (sortflag == 1)
  185.     l = archie_query(host,str,max_hits,offset,query,AQ_INVDATECMP,0);
  186.     else
  187.     l = archie_query(host,str,max_hits,offset,query,NULL,0);
  188.  
  189.     /* Error? */
  190.     if (perrno != PSUCCESS) {
  191.     if (p_err_text[perrno]) {
  192.         if (*p_err_string)
  193.         fprintf(stderr, "%s: failed: %s - %s\n",
  194.             program_name, p_err_text[perrno], p_err_string);
  195.         else
  196.             fprintf(stderr, "%s failed: %s\n",
  197.             program_name, p_err_text[perrno]);
  198.     } else
  199.         fprintf(stderr, "%s failed: Undefined error %d (prospero)",
  200.             program_name, perrno);
  201.     }
  202.  
  203.     /* Warning? */
  204.     if (pwarn != PNOWARN) {
  205.     if (*p_warn_string)
  206.         fprintf(stderr, "%s: Warning! %s - %s\n", program_name,
  207.         p_warn_text[pwarn], p_warn_string);
  208.     else
  209.         fprintf(stderr, "%s: Warning! %s\n", program_name, p_warn_text[pwarn]);
  210.     }
  211.  
  212.  
  213.     /* Display the results */
  214.  
  215.     if (l == (VLINK)NULL && pwarn == PNOWARN && perrno == PSUCCESS) {
  216.     if (! listflag) puts ("No matches.");
  217. #ifdef CUTCP
  218.     netshut();
  219. #endif
  220.     exit (1);
  221.     }
  222.  
  223.     *lasthost = '\001';
  224.     *lastpath = '\001';
  225.     while (l != NULL) {
  226.     display_link (l);
  227.     l = l->next;
  228.     }
  229. }
  230.  
  231. /* Given a dotted hostname, return its Alex root.  */
  232. static char *
  233. alex_reverse (string, len)
  234.      char *string;
  235.      int len;
  236. {
  237.   register char *p, *q;
  238.   register int i = 0;
  239.   char *buf = (char *) malloc (len);
  240.  
  241.   p = buf;
  242.   q = (char *) (string + len - 1);
  243.   while (q > string)
  244.     {
  245.       for (i = 0; q > string; q--, i++)
  246.     if (*q == '.')
  247.       {
  248.         q++;
  249.         break;
  250.       }
  251.       if (q == string)
  252.     i++;
  253.       strncpy (p, q, i);
  254.       p += i;
  255.       *p++ = '/';
  256.       i = 0;
  257.       q -= 2;
  258.     }
  259.   *--p = '\0';
  260.   return buf;
  261. }
  262.  
  263. /* Emit a string that's the Alex filename for the given host and file.  */
  264. static void
  265. alex_name (host, file, dirp)
  266.      char *host, *file;
  267.      int dirp;
  268. {
  269.   int hostlen = strlen (host);
  270.   int len = 6 + hostlen + strlen (file) + dirp +  1;
  271.   char *buf = (char *) malloc (len);
  272.  
  273.   if (! buf)
  274.     exit (99);
  275.  
  276.   sprintf (buf, "/alex/%s%s", alex_reverse (host, hostlen), file);
  277.   if (dirp)
  278.     {
  279.       len -= dirp;
  280.       buf[len - 1] = '/';
  281.       buf[len] = '\0';
  282.     }
  283.  
  284.   fputs (buf, archie_out);
  285. }
  286.